﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GameSchoolApplication.Models;

namespace GameSchoolApplication.Repositories
{
    public class NotificationRepository
    {
        gsDBDataContext link = new gsDBDataContext();

        /// <summary>
        /// Getting notifications based on ID
        /// Precondition:
        /// Postcondition: A notification is returned
        /// Author: Aron/Bjarmi mod. by Alma
        /// Date: 7.5. 2012
        /// </summary>
        public Notification getNotificationByID(int notifID)
        {
            Notification selectedNotif = (from n in link.Notifications
                               where n.ID == notifID
                               select n).First();            
            return selectedNotif; 
        }

        /// <summary>
        /// Returns a list of all notifications with a certain relation. 
        /// Precondition: 
        /// Postcondition: A list of notifications is returned. 
        /// Author: Krilli
        /// Date: 8.5
        /// </summary>
        /// <param name="relationID">The ID of the relation in question</param>
        /// <returns>A list of notifications</returns>
        public IQueryable<Notification> getRelationNotifications(int relationID)
        {
            IQueryable<Notification> notifications = (from a in link.Notifications
                                                      where a.relationID == relationID
                                                      select a);
            return notifications; 
        }

        /// <summary>
        /// Returns a list of notifications
        /// Precondition:
        /// Postcondition: A list of all notifications is returned
        /// Author: Aron/Bjarmi mod. by Alma
        /// Date: 7.5.2012
        /// </summary>
        public IQueryable<Notification> getAllNotifications()
        {
            return link.Notifications;
        }

        /// <summary>
        /// Adding a notification
        /// Precondition: 
        /// Postcondition: A notification has been added
        /// Author: Aron/Bjarmi mod. by Alma
        /// Date: 7.5.2012
        /// </summary>
        public bool addNotification(Notification notif)
        {
            try
            {
                link.Notifications.InsertOnSubmit(notif);
            }
            catch (Exception)
            {
                return false;
            }
            return true; 
        }

        /// <summary>
        /// Editing notifications 
        /// Precondition: User must be an administrator/teacher and notification must exist
        /// Postcondition: Account information has been updated in the database
        /// Author: Aron Mar mod. by Alma
        /// Date: 
        /// </summary>
        public bool editNotification(Notification notif)
        {
            try
            {
                var noti = (from n in link.Notifications
                            where n.ID == notif.ID
                            select n).First();

                noti.eventType = notif.eventType;
                noti.dateAdded = notif.dateAdded;
                noti.relationID = notif.relationID;

                save();

                return true;
            }
            catch (Exception)
            {
                return false;
            }
            
        }

        /// <summary>
        /// Saves changes to database
        /// Precondition:
        /// Postcondition: Database has been updated
        /// Author: Aron og Bjarmi
        /// Date: 03.05.2012
        /// </summary>
        public void save()
        {
            link.SubmitChanges();
        }

    }
}